import router from './router'
import store from './store'
import { getToken } from '@/utils/cookies' // 验权
const whiteList = ['/login', '/404'] //白名单,不需要登录的路由
router.beforeEach(async(to,from,next) => {
  let token = sessionStorage.getItem('token')
    if (!!token) {
      console.log('有token',store.getters.nickname,'store.getters.nickname')
        //已登录
        if (to.path === 'login') {
            next('/login')
        }else if (!store.getters.nickname) {
          try {
            await store.dispatch('GetInfo')
            next({...to})
          } catch (error) {
            console.log(error,'获取用户信息出错GetInfo error')
            sessionStorage.removeItem('token')
          }
        }else {
          next()
        }
    } else if (whiteList.indexOf(to.path) !== -1) { //前往白名单 放行
        next()
    } else { //如果路径不是白名单内的,而且又没有登录,就跳转登录页面
        store.commit('RESET_USER')
        next('/login')
    }
})
